package com.hzit.loan.pay.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hzit.loan.pay.commonApi.enums.PayStatusEnum;
import com.hzit.loan.pay.commonApi.enums.PayWayEnum;
import com.hzit.loan.pay.commonApi.resp.PayResultData;
import com.hzit.loan.pay.commonApi.resp.RespVo;
import com.hzit.loan.pay.commonApi.utils.PayDigestUtil;
import com.hzit.loan.pay.config.PayConfig;
import com.hzit.loan.pay.factory.PayStrategyFactory;
import com.hzit.loan.pay.iClient.IAlipayFrontClient;
import com.hzit.loan.pay.mapper.MchInfoMapper;
import com.hzit.loan.pay.mapper.PaySerialMapper;
import com.hzit.loan.pay.model.MchInfo;
import com.hzit.loan.pay.model.PaySerial;
import com.hzit.loan.pay.service.IPayStrategyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * 支付宝扫码支付
 */
@Service
public class AliapySmPayImpl implements IPayStrategyService, InitializingBean {

    @Autowired
    private PayConfig payConfig;

    @Autowired
    private MchInfoMapper mchInfoMapper;

    @Autowired
    private PaySerialMapper paySerialMapper;


    @Autowired
    private IAlipayFrontClient alipayFrontClient;

    private Logger logger = LoggerFactory.getLogger(AliapySmPayImpl.class);

    /**
     * 调用支付宝前置服务中的二维码支付接口
     * @param paySerial
     * @return
     */
    @Override
    public RespVo<PayResultData> pay(PaySerial paySerial) {

        //访问前置服务

        RespVo<PayResultData>  respVo =   alipayFrontClient.qrCodePay(paySerial.getSubject(),
                paySerial.getPaySerialNo(),paySerial.getMchOrderAmount().toString());

        try {

            if(0 == respVo.getCode()){
                // 更新流水为支付中
                PaySerial record = new PaySerial();
                record.setUpdateTime(new Date());
                record.setStatus(PayStatusEnum.PAY_ING.getCode().toString());
                paySerialMapper.updateByPrimaryKeySelective(record);
            }

            PayResultData payResultData =  respVo.getData();
            //TODO
            payResultData.setPayWay(paySerial.getPayWay());
            payResultData.setMchId(paySerial.getMchId());

            //返回参数签名
            MchInfo mchInfo = mchInfoMapper.queryMchInfoByMchId(paySerial.getMchId());
            Map map = JSONObject.parseObject(JSONObject.toJSONString(payResultData), Map.class);
            String sign = PayDigestUtil.getSign(map,mchInfo.getRespKey(),"sign");
            payResultData.setSign(sign);
            respVo.setData(payResultData);
            return respVo;

        } catch (Exception e) {
            logger.error("Exception",e);
            return RespVo.getFailReturn(e.getMessage());
        }


    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("=======AliapySmPayImpl初始化=========");
        PayStrategyFactory.concurrentHashMap.put(PayWayEnum.ALIPAY_SM.getCode(),this);

    }
}
